package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;

public class Player extends AnimatedSprite{
	
	private static float speed;
	private static final long serialVersionUID = 1L;
	public final float HPMax = 100;
	public float HP = 100;
	private int quantTiroFixo = 30;
	private int quantTiro = quantTiroFixo;
	private boolean carregandoArma = false;
	private float timeCarregamentoArmaFixo = 3;
	private float timeCarregamentoArma = timeCarregamentoArmaFixo;
	private Point sizePersonagem = new Point(50, 50);
	private static double cosseno;
	private static double seno;
	
	
	public static float getSpeed() {
		return speed;
	}
	public static void setSpeed(float speed) {
		Player.speed = speed;
		seno = (double)(Math.sin(45) * speed);
		cosseno = (double)(Math.cos(45) * speed);
	}
	
	public float getQuantTiro() {
		return quantTiro;
	}
	public float getHPMax() {
		return HPMax;
	}
	public float getHP() {
		return HP;
	}
	public void setHP(float hp) {
		HP = hp;
	}
	public void addHP(float hp) {
		HP += hp;
	}


	public Player(int index, Point2D pos, Point2D dim, Point2D sPos, Point2D sDim)
	{
		super(index, pos, dim, sPos, sDim, 0.2f, new Point(1, 3), true);
		setSpeed(3);
	}
	
	public void update()
	{
		ShootManager.getInstance().update();
		
		
		
		if (Keyboard.getInstance().isKeyPressed(KeyEvent.VK_W))
		{
			this.animate = true;
			this.currentFrame.x = 1;
			
			if (Keyboard.getInstance().isKeyPressed(KeyEvent.VK_A))
			{
				this.setPosition(new Point2D.Double(this.position.getX() - cosseno, this.position.getY() - seno));
			}
			else if (Keyboard.getInstance().isKeyPressed(KeyEvent.VK_D))
			{
				this.setPosition(new Point2D.Double(this.position.getX() + cosseno, this.position.getY() - seno));
			}
			else
			{
				this.setPosition(new Point2D.Double(this.position.getX(), this.position.getY() - speed));
			}
		}
		else if (Keyboard.getInstance().isKeyPressed(KeyEvent.VK_S))
		{

			this.animate = true;
			this.currentFrame.x = 1;
			
			if (Keyboard.getInstance().isKeyPressed(KeyEvent.VK_A))
			{
				this.setPosition(new Point2D.Double(this.position.getX() - cosseno, this.position.getY() + seno));
			}
			else if (Keyboard.getInstance().isKeyPressed(KeyEvent.VK_D))
			{
				this.setPosition(new Point2D.Double(this.position.getX() + cosseno, this.position.getY() + seno));
			}
			else
			{
				this.setPosition(new Point2D.Double(this.position.getX(), this.position.getY() + speed));
			}
			
		}
		else if (Keyboard.getInstance().isKeyPressed(KeyEvent.VK_A))
		{
			this.setPosition(new Point2D.Double(this.position.getX() - speed, this.position.getY()));
		}
		else if (Keyboard.getInstance().isKeyPressed(KeyEvent.VK_D))
		{
			this.setPosition(new Point2D.Double(this.position.getX() + speed, this.position.getY()));
		}
			
		
		
		if (quantTiro > 0 && !this.carregandoArma)
		{
			if(Mouse.getInstance().isMousePressed(MouseEvent.BUTTON1))
			{
				if(ShootManager.getInstance().Fire(this.position, this.dimension, this.rotation))
				{
					this.quantTiro--;
					if(quantTiro == 0)
						this.carregandoArma = true;
				}
			}
			if(Keyboard.getInstance().isKeyPressed(KeyEvent.VK_R) && this.quantTiro != this.quantTiroFixo)
				this.carregandoArma = true;
		}
		
		if(carregandoArma)
		{
			
			this.timeCarregamentoArma -= Game.loopTime;
			if(timeCarregamentoArma <= 0)
			{
				Audios.getInstance().Play(0);
				this.carregandoArma = false;
				this.quantTiro = this.quantTiroFixo;
				this.timeCarregamentoArma = this.timeCarregamentoArmaFixo;
			}
		}
		
		ShootManager.getInstance().CollisionWithPlayer(this);
		
		LimitBound();
		this.rotation = lookAt(this.position, Mouse.getInstance().getPositionMouse(), this.getOrigin());
		//System.out.println("ROTATION: " + rotation);
		
		if (this.HP <= 0)
		{
			Audios.getInstance().Play(6);
		}
		
		super.update();
	}
	
	public void LimitBound()
	{
		if (position.getX() > 800 - dimension.getX())
			position = new Point2D.Double(800 - dimension.getX(), position.getY());
		if (position.getY() > 600 - dimension.getY())
			position = new Point2D.Double(position.getX(), 600 - dimension.getY());
		if (position.getX() < 0)
			position = new Point2D.Double(0, position.getY());
		if (position.getY() < 0)
			position = new Point2D.Double(position.getX(), 0);
	}
	
	public void draw(Graphics g)
	{
		ShootManager.getInstance().draw(g);
		
		if(this.carregandoArma)
		{
			g.setColor(Color.BLACK);
			g.fillRect((int)this.getPosition().getX(), (int)this.getPosition().getY() - 8, (int)this.sizePersonagem.getX(), 4);
			
			g.setColor(Color.WHITE);
			g.fillRect((int)this.getPosition().getX() + 1, (int)this.getPosition().getY() - 7, (int)((this.sizePersonagem.getX() - 1)/(this.timeCarregamentoArmaFixo/this.timeCarregamentoArma)), 2);
		}
		
		super.draw(g);
	}
	
	private float lookAt(Point2D p1, Point2D p2, Point2D origin)
    {
        float diffX = (float)(p1.getX() + origin.getX() - p2.getX());
        float diffY = (float)(p1.getY() + origin.getY() - p2.getY());
        float result = (float)Math.toDegrees((float)Math.atan2(diffY, diffX));
        result = (result - 180) % 360;
        if (result < 0)
            result += 360;
        return result;
    }
}
